﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QLVT.Entity;
using QLVT.Config;

namespace QLVT.DAO
{
    class KhoDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from Kho");
        }

        public Kho GetBymakho(String makho)
        {
            Kho kho = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from Kho where makho = '" + makho + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    kho = new Kho();
                    kho.Makho = dataReader.GetString(0);
                    kho.Tenkho = dataReader.GetString(1);
                    return kho;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
               
            }
            return kho;

        }
        public bool Insert(Kho kho)
        {
            String sql = "INSERT INTO [QLVT].[dbo].[kho]([makho],[tenkho])"
               + "VALUES('" + kho.Makho + "','" + kho.Tenkho  + "')";
            return Update(sql);
        }

        public bool DeleteBymakho(String makho)
        {
            return Update("delete from Kho where makho = '" + makho + "'");
        }

        public bool UpdateBymakho(Kho kho)
        {
            String sql = "update Kho "
                 + " set tenkho = '" + kho.Tenkho + "'"
                 + " where makho = '" + kho.Makho + "'";
            return Update(sql);
        }
    }
}
